import { ApidepartmentList } from "../../../../api/index"
const app = getApp()
Component({
  lifetimes: {
    attached() {
      this.init(this.data.words)
    }
  },
  properties: {
    words: {
      type: String,
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    loading: false,
    list: [],
    activeTab: 0,
    name: "",
    activelist: null,
    $path: app.globalData.$path
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init(words) {
      this.setData({
        list: [],
        activeTab: 0,
        name: "",
        activelist: null,
      },() => {
        const ops = {}
        if(words) ops.words = words
        this.getList(ops)
      })
    },
    getList(ops) {
      this.setData({
        loading: true
      })
      ApidepartmentList(ops).then(res => {
        const list = res.data.map(item => {
          const len = item.name.length
          if(len > 6) item.fontSize = 20
          else {
            item.fontSize = 26 - len 
          }
          item.fontSize += "rpx"
          return {
            ...item
            
          }
        })
        this.setData({
          list,
          loading: false
        })
        this.getActiveList()
      })
    },

    onChange(e){
      const index = e.currentTarget.dataset.index
      if(index !== this.data.activeTab) {
        this.setData({
          activeTab: index
        })
        this.getActiveList()
      }
    },

    getActiveList(){
      if(!this.data.list[this.data.activeTab]) return
      const {info , name} = this.data.list[this.data.activeTab]
      this.setData({
        activelist: info,
        name: name 
      })
    }
  }
})